package org.oslims.client.uicomponents.inputfields;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class TextInputField extends Composite implements InputField {

	final HorizontalPanel fieldLine = new HorizontalPanel();
	final Label fieldLabel = new Label();
	final TextBox fieldBox = new TextBox();
	final Label fieldErrorLabel = new Label();
	
	private String fieldName;
	
	public TextInputField(String fieldName, String fieldCaptation, String labelSize, String boxSize) {
		
		this.fieldName = fieldName;
		
		fieldLabel.setText(fieldCaptation);
		fieldLabel.setWidth(labelSize);
		fieldLine.add(fieldLabel);
		
		fieldBox.setWidth(boxSize);
		
		fieldLine.add(fieldBox);
		
		fieldErrorLabel.setVisible(false);
		fieldLine.add(fieldErrorLabel);
		
		initWidget(fieldLine);
	}

	public String getFieldName() {
		return fieldName;
	}
	
	public void showError(String errorMessage) {
		fieldErrorLabel.setText(errorMessage);
		fieldErrorLabel.setVisible(true);
	}
	
	public void clearError() {
		fieldErrorLabel.setText("");
		fieldErrorLabel.setVisible(false);
	}
	
	@SuppressWarnings("unchecked")
	public HasValue<String> getInputField() {
		return fieldBox;
	}
}
